package sql;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import sql.record.Course;
import sql.record.CourseSchedule;
import sql.record.Registration;
import sql.record.TableRecord;
import sql.record.User;

public class SearchUtilityTest {

	User toInsert = null;
	Course nihul = null;
	CourseSchedule hours = null;
	
	@Before
	public void init() throws ClassNotFoundException, SQLException {
		
		toInsert = new User();
		toInsert.givenName = "Artem";
		toInsert.phoneNumber = "0123";
		toInsert.surName = "Merkovich";
		toInsert.type = 0;
		toInsert.userName = "1";
		
		DBManagment.createDB();
		DBManagment.createTable(toInsert);
		List<TableRecord> toInsertList = new LinkedList<TableRecord>();
		toInsertList.add(toInsert);
		DBManagment.insertRecord(toInsertList);
		
		// creating a course
		nihul = new Course();
		nihul.courseName = "nihul";
		nihul.capacity = 1;
		nihul.creditPoints = 3;
		nihul.description = "great course";
		DBManagment.createTable(nihul);
		
		// creating it's hours
		hours = new CourseSchedule();
		hours.courseName = "nihul";
		hours.day = 1; // sunday
		hours.endHour = 12;
		hours.startHour = 8;
		hours.groupNum = 3;
		DBManagment.createTable(hours);
		
		// adding to the db's
		List<TableRecord> records = new LinkedList<TableRecord>();
		records.add(nihul);
		records.add(hours);
		DBManagment.insertRecord(records);
	}
	
	@After
	public void clean() throws ClassNotFoundException, SQLException {
		
		DBManagment.destroyTable(toInsert);
		DBManagment.destroyTable(nihul);
		DBManagment.destroyTable(hours);
	}
	
	@Test
	public void basicSearchTest() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("");
		System.out.println("Test 0 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(2, SearchUtility.searchDB("1", keywords).size());
	}
	
	@Test
	public void basicSearchTest2() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		System.out.println("Test 1 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(0, SearchUtility.searchDB("1", keywords).size());
	}
	
	@Test
	public void basicSearchTest3() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("SomeTextThatShouldntGetResults");
		System.out.println("Test 3 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(0, SearchUtility.searchDB("1", keywords).size());
	}
	
	
	@Test
	public void basicSearchTest4() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("nihul");
		System.out.println("Test 4 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(2, SearchUtility.searchDB("1", keywords).size());
	}
	
	@Test
	public void basicSearchTest5() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("3");
		System.out.println("Test 5 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(2, SearchUtility.searchDB("1", keywords).size());
	}
	
	@Test
	public void basicSearchTest6() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("great");
		System.out.println("Test 6 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(2, SearchUtility.searchDB("1", keywords).size());
	}

	@Test
	public void basicSearchTest7() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		keywords.add("Monday");
		System.out.println("Test 6 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(2, SearchUtility.searchDB("1", keywords).size());
	}
	
	private void aux_add_course(int amount) throws ClassNotFoundException, SQLException{
		List<TableRecord> records = new LinkedList<TableRecord>();
		
		String incWord = ""; 
		for(int i = 1; i < amount+1; i++){
			incWord += (new Integer(i)).toString(); 
			// creating a course
			Course course = new Course();
			course.courseName = "nihul" + incWord;
			course.capacity = 1;
			course.creditPoints = 3;
			course.description = "great course";
			
			// creating it's hours
			CourseSchedule cSched = new CourseSchedule();
			cSched.courseName = "nihul" + incWord;
			cSched.day = i + 1; 
			cSched.endHour = 12;
			cSched.startHour = 8;
			cSched.groupNum = 3;
	
			records.add(course);
			records.add(cSched);
		}
		DBManagment.insertRecord(records);
	}
	
	@Test
	public void orderTest() throws ClassNotFoundException, SQLException{
		List<String> keywords = new LinkedList<String>();
		aux_add_course(3);
		keywords.add("great");
		keywords.add("1");
		keywords.add("2");
		keywords.add("3");
		keywords.add("4");
		System.out.println("Test 8 Size is: " + SearchUtility.searchDB("1", keywords).size());
		System.out.println("Contants:");
		for(TableRecord t:SearchUtility.searchDB("1", keywords)){
			System.out.println(t);
		}
		assertEquals(8, SearchUtility.searchDB("1", keywords).size());
	}
}
